library(foreign)
library(dplyr)
library(readstata13)
library(ggplot2)
library(ggeffects)
library(ggExtra)
library(cowplot)
library(patchwork)
library(broom)

rm(list=ls())

#all figure and table numbers reference the APSR Dataverse Appendix

#load data
load("RGC_Contribution_Experiment_Data.rdata")

####manuscript figure number 4####
blackorgresp<-subset(df, suppb >= 0)
whiteorgresp<-subset(df, suppw >= 0)

m1giveblack<-lm(contb ~ rgcmean+mturk, data = blackorgresp)
m1supportblack<-lm(suppb ~ rgcmean+mturk, data = blackorgresp)

m2givewhite<-lm(contw ~ rgcmean+mturk, data = whiteorgresp)
m2supportwhite<-lm(suppw ~ rgcmean+mturk, data = whiteorgresp)

m1giveblackout <- tidy(m1giveblack, conf.int = TRUE, conf.level = .95)
m1giveblackout <- subset(m1giveblackout, term == "rgcmean")
m1giveblackout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m1supportblackout <- tidy(m1supportblack, conf.int = TRUE, conf.level = .95)
m1supportblackout <- subset(m1supportblackout, term == "rgcmean")
m1supportblackout$group <- "Predicted Value of RGC on 0-10 Support"

combined<-rbind(m1giveblackout, m1supportblackout)
combined$estimation <- combined$group
combined$groups[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combined$groups[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "
combined$organization <- "Black Organization"
m2givewhiteout <- tidy(m2givewhite, conf.int = TRUE, conf.level = .95)
m2givewhiteout <- subset(m2givewhiteout, term == "rgcmean")
m2givewhiteout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m2supportwhiteout <- tidy(m2supportwhite, conf.int = TRUE, conf.level = .95)
m2supportwhiteout <- subset(m2supportwhiteout, term == "rgcmean")
m2supportwhiteout$group <- "Predicted Value of RGC on 0-10 Support"

combinedwhite<-rbind(m2givewhiteout, m2supportwhiteout)
combinedwhite$estimation <- combinedwhite$group
combinedwhite$groups[combinedwhite$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combinedwhite$groups[combinedwhite$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "
combinedwhite$organization <- "Race-Neutral Organization"
combinedplot<-rbind(combined, combinedwhite)
combinedplot$Organization <- combinedplot$organization
library(cowplot)
library(stringr)
p <- ggplot(data = combinedplot, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p<-p + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(color=Organization),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip()+
  scale_x_discrete(labels = c("RGC on $0-$10 Contribution", 
                              "RGC on 0-10 Support")) +
  scale_y_continuous(breaks=c(-5,-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                     limits = c(-5, 10)) +
  theme_classic() +
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  labs(title = "RGC and Monetary Contribution Experiment", 
       caption = "",  
       x = "", y = "0-10 Support/$0-$10 Contribution") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) 

p

###this code estimates the cronbach's alpha of rgc scale###
##for codebook table 3#
library(ltm)
myvars <- c("ridimp", "selfimage", "impself", 
            "blackfate", "autonomy", "ftblack",
            "close","discrim","rrdiscrim",
            "keptback", "power", "collective",
            "votediff", "blackorg", "blackrights", 
            "workhardindv", "linkfate")

ndexp <- df[myvars]

cronbach.alpha(ndexp, na.rm = TRUE)


######Appendix table A.4######
#this code estimates the Relationship between RGC and Organizational 
#Supportiveness by Racial Empowerment Treatment#
#Tabular Results for Figure 4#
stargazer::stargazer(m2supportwhite, m1supportblack, m2givewhite, m1giveblack, type = "html", out="tablea4.doc")

###Appendix table A.14####
##black org support##
mean(df$suppb, na.rm = TRUE)
#race neutral org support##
mean(df$suppw, na.rm = TRUE)
##black org contribution##
mean(df$contb, na.rm = TRUE)
#race neutral org contribution##
mean(df$contw, na.rm = TRUE)

###likelihood of giving by condition###
df$givingintention[df$q179 >= 3] <- 0
df$givingintention[df$q179 < 3] <- 1

mean(df$givingintention[df$suppb >= 0], na.rm = TRUE)
mean(df$givingintention[df$suppw >= 0], na.rm = TRUE)

summary(df$contb)
summary(df$suppb)

summary(df$contw)
summary(df$suppw)

###correlation between support and giving###
cor(df$suppb, df$contb, use = "pairwise.complete.obs")
cor(df$suppw, df$contw, use = "pairwise.complete.obs")

###rgc mean uses the following items to construct RGC scale###
####: racial id importance, racial group as self-image, importance of race to self identity
##black linked fate, racial autonomy, racial group feeling therm, racial group closeness, discrimination
##racial group power, racial groups should work collectively, racial groups should vote together, black group rights##
df$rgcmean <- (rowMeans(df[c("ridimp", "selfimage", "impself", 
                                                   "blackfate", "autonomy", "ftblack",
                                                   "close","discrim","rrdiscrim",
                                                   "keptback", "power", "collective",
                                                   "votediff", "blackorg", "blackrights", 
                                                   "workhardindv", "linkfate")], na.rm=TRUE))
summary(df$rgcmean)


###Appendix table A.19####
#relationship between giving and survey platform#
summary(df$contb[df$mturk == 1])
summary(df$contw[df$mturk == 1])
summary(df$contb[df$mturk == 0])
summary(df$contw[df$mturk == 0])

###graph###
###Appendix figure A.10####
#predicted effect of number of days on support and contribution##
#Appendix Table A.16
dayssuport <- lm(suppb ~ totnumday, data = df)
dayscontribution <- lm(contb ~ totnumday, data = df)

mblackdaysgive <- tidy(dayscontribution, conf.int = TRUE, conf.level = .95)
mblackdaysgive <- subset(mblackdaysgive, term == "totnumday")
mblackdaysgive$group <- "Predicted effect of number of days on $0-$10 Contribution"

mblackdayssupport <- tidy(dayssuport, conf.int = TRUE, conf.level = .95)
mblackdayssupport <- subset(mblackdayssupport, term == "totnumday")
mblackdayssupport$group <- "Predicted effect of number of days on 0-10 Support"

combined<-rbind(mblackdaysgive, mblackdayssupport)
combined$estimation <- combined$group
combined$groups[combined$group == "Predicted effect of number of days on 0-10 Support"] <- "   "
combined$groups[combined$group == "Predicted effect of number of days on $0-$10 Contribution"] <- " "

p <- ggplot(data = combined, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p<-p + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation), position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-1, -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1), limits = c(-1, 1)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=10))+
  labs(title = "Black Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p

#Appendix Table A.16
dayssuportyt <- lm(suppw ~ totnumday, data = df)
dayscontributionyt <- lm(contw ~ totnumday, data = df)

mwhitecontribution <- tidy(dayscontributionyt, conf.int = TRUE, conf.level = .95)
mwhitecontribution <- subset(mwhitecontribution, term == "totnumday")
mwhitecontribution$group <- "Predicted effect of number of days on $0-$10 Contribution"

mwhitesupport <- tidy(dayssuportyt, conf.int = TRUE, conf.level = .95)
mwhitesupport <- subset(mwhitesupport, term == "totnumday")
mwhitesupport$group <- "Predicted effect of number of days on 0-10 Support"

combined2<-rbind(mwhitecontribution, mwhitesupport)
combined2$estimation <- combined2$group
combined2$groups[combined$group == "Predicted effect of number of days on $0-$10 Contribution"] <- "   "
combined2$groups[combined$group == "Predicted effect of number of days on 0-10 Support"] <- " "

p2 <- ggplot(data = combined2, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p2<-p2 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-1, -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1), limits = c(-1, 1)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=10))+
  labs(title = "Race-Neutral Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2))


p2

pa<-cowplot::plot_grid(p, p2, align = "h", axis = "bt")
pa

pas<-cowplot::plot_grid(p + theme(legend.position="none"), 
                        p2+ theme(legend.position="none"), ncol = 2)
legend_b <- get_legend(p  +
                         theme(legend.position = "bottom"))

cowplot::plot_grid(pas, legend_b, ncol = 1, rel_heights = c(1, .15))
ggsave2("appendixa5.jpeg")
###Appendix figure A.11#####
#predicted effect of pre-post design, number of days on support, 
#pre-post same day, and pre-post 4 days gap###
mturk <- subset(df, mturk == 1)

mturksameday <- subset(mturk, totnumday == 0)
mturk4plus <- subset(mturk, totnumday >= 4)

#Appendix Table A.17
mturksamedaysupport <- lm(suppb ~ rgcmean, data = mturksameday)
mturksamedaycontribute <- lm(contb ~ rgcmean, data = mturksameday)
mturksamedaysupportneutral <- lm(suppw ~ rgcmean, data = mturksameday)
mturksamedaycontributeneutral <- lm(contw ~ rgcmean, data = mturksameday)

#Appendix Table A.18
mturk4daysupport <- lm(suppb ~ rgcmean, data = mturk4plus)
mturk4daycontribute <- lm(contb ~ rgcmean, data = mturk4plus)
mturk4daysupportneutral <- lm(suppw ~ rgcmean, data = mturk4plus)
mturk4daycontributeneutral <- lm(contw ~ rgcmean, data = mturk4plus)

mblackdaysgive <- tidy(mturksamedaycontribute, conf.int = TRUE, conf.level = .95)
mblackdaysgive <- subset(mblackdaysgive, term == "rgcmean")
mblackdaysgive$group <- "Predicted Value of RGC on $0-$10 Contributed"
mblackdayssupport <- tidy(mturksamedaysupport, conf.int = TRUE, conf.level = .95)
mblackdayssupport <- subset(mblackdayssupport, term == "rgcmean")
mblackdayssupport$group <- "Predicted Value of RGC on 0-10 Support"

combined<-rbind(mblackdayssupport, mblackdaysgive)
combined$estimation <- combined$group
combined$group[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "1"
combined$group[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- "2"

p <- ggplot(data = combined, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high))
p<-p + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.1), size=1)+
  coord_flip() +
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-9, 14)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank()) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank())+
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Black Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p

mneutralaysgive <- tidy(mturksamedaycontributeneutral, conf.int = TRUE, conf.level = .95)
mneutralaysgive <- subset(mneutralaysgive, term == "rgcmean")
mneutralaysgive$group <- "Predicted Value of RGC on $0-$10 Contributed"
mneutraldayssupport <- tidy(mturksamedaysupportneutral, conf.int = TRUE, conf.level = .95)
mneutraldayssupport <- subset(mneutraldayssupport, term == "rgcmean")
mneutraldayssupport$group <- "Predicted Value of RGC on 0-10 Support"

combined2<-rbind(mneutraldayssupport, mneutralaysgive)
combined2$estimation <- combined2$group
combined2$group[combined2$group == "Predicted Value of RGC on 0-10 Support"] <- "1"
combined2$group[combined2$group == "Predicted Value of RGC on $0-$10 Contributed"] <- "2"

p2 <- ggplot(data = combined2, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high))
p2<-p2 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.1), size=1)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-9, 14)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank())+
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Race-Neutral Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p2

mblackdaysgive2 <- tidy(mturk4daycontribute, conf.int = TRUE, conf.level = .95)
mblackdaysgive2 <- subset(mblackdaysgive2, term == "rgcmean")
mblackdaysgive2$group <- "Predicted Value of RGC on $0-$10 Contributed"
mblackdayssupport2 <- tidy(mturk4daysupport, conf.int = TRUE, conf.level = .95)
mblackdayssupport2 <- subset(mblackdayssupport2, term == "rgcmean")
mblackdayssupport2$group <- "Predicted Value of RGC on 0-10 Support"

combined3<-rbind(mblackdayssupport2, mblackdaysgive2)
combined3$estimation <- combined3$group
combined3$group[combined3$group == "Predicted Value of RGC on 0-10 Support"] <- "1"
combined3$group[combined3$group == "Predicted Value of RGC on $0-$10 Contributed"] <- "2"

p3 <- ggplot(data = combined3, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high))
p3<-p3 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.1), size=1)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-9, 16)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank())+
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=20))+
  theme(legend.text = element_text(size=20))+
  labs(title = "Black Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p3

mneutralaysgive2 <- tidy(mturk4daycontributeneutral, conf.int = TRUE, conf.level = .95)
mneutralaysgive2 <- subset(mneutralaysgive2, term == "rgcmean")
mneutralaysgive2$group <- "Predicted Value of RGC on $0-$10 Contributed"
mneutraldayssupport2 <- tidy(mturk4daysupportneutral, conf.int = TRUE, conf.level = .95)
mneutraldayssupport2 <- subset(mneutraldayssupport2, term == "rgcmean")
mneutraldayssupport2$group <- "Predicted Value of RGC on 0-10 Support"

combined4<-rbind(mneutraldayssupport2, mneutralaysgive2)
combined4$estimation <- combined4$group
combined4$group[combined4$group == "Predicted Value of RGC on 0-10 Support"] <- "1"
combined4$group[combined4$group == "Predicted Value of RGC on $0-$10 Contributed"] <- "2"

p4 <- ggplot(data = combined4, aes(x = group, y = estimate, ymin = conf.low, ymax = conf.high))
p4<-p4 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.1), size=1)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-9, 14)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank())+
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Race-Neutral Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p4
library(patchwork)
combinedplot <- (p + p2) / (p3 + p4) & theme(legend.position = "bottom")+
  theme(plot.subtitle = element_text(hjust = 0), plot.caption = element_text(hjust = 0))
combinedplot + plot_layout(guides = "collect")+plot_annotation(
  subtitle = 'Pre and post same day',
  caption = 'At least 4 days between pre and post'
)
ggsave2("appendixa6.jpeg")

###Appendix figure A.12####
#relationship between rgc and support/contribution for mturk respondents#
mturk <- subset(df, mturk == 1)

#Appendix Table A.20
mturkblacksupport <- lm(suppb ~ rgcmean, data = mturk)
mturkblackcontribution <- lm(contb ~ rgcmean, data = mturk)

m1giveblackout <- tidy(mturkblackcontribution, conf.int = TRUE, conf.level = .95)
m1giveblackout <- subset(m1giveblackout, term == "rgcmean")
m1giveblackout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m1supportblackout <- tidy(mturkblacksupport, conf.int = TRUE, conf.level = .95)
m1supportblackout <- subset(m1supportblackout, term == "rgcmean")
m1supportblackout$group <- "Predicted Value of RGC on 0-10 Support"

combined<-rbind(m1giveblackout, m1supportblackout)
combined$estimation <- combined$group
combined$groups[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combined$groups[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "

p <- ggplot(data = combined, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p<-p + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-7, 11)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Black Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p

#Appendix Table A.20
mturkneutralsupport <- lm(suppw ~ rgcmean, data = mturk)
mturkneutralcontribution <- lm(contw ~ rgcmean, data = mturk)

m1giveneutralout <- tidy(mturkneutralcontribution, conf.int = TRUE, conf.level = .95)
m1giveneutralout <- subset(m1giveneutralout, term == "rgcmean")
m1giveneutralout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m1supportneutralout <- tidy(mturkneutralsupport, conf.int = TRUE, conf.level = .95)
m1supportneutralout <- subset(m1supportneutralout, term == "rgcmean")
m1supportneutralout$group <- "Predicted Value of RGC on 0-10 Support"

combined2<-rbind(m1giveneutralout, m1supportneutralout)
combined2$estimation <- combined$group
combined2$groups[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combined2$groups[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "

p2 <- ggplot(data = combined2, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p2<-p2 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6,-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-7, 11)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Race-Neutral Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p2

pa<-cowplot::plot_grid(p, p2, align = "h", axis = "bt")
pa

pas<-cowplot::plot_grid(p + theme(legend.position="none"), 
                        p2+ theme(legend.position="none"), ncol = 2)
legend_b <- get_legend(p  +
                         theme(legend.position = "bottom"))

cowplot::plot_grid(pas, legend_b, ncol = 1, rel_heights = c(1, .15))
ggsave2("appendixa7.jpeg")



###Appendix figure A.13###
#lucid only respondents, effects#
#relationship between rgc and support/contribution for lucid respondents#
lucid <- subset(df, mturk == 0)

#Appendix Table A.21
lucidblacksupport <- lm(suppb ~ rgcmean, data = lucid)
lucidblackcontribution <- lm(contb ~ rgcmean, data = lucid)
lucidneutralsupport <- lm(suppw ~ rgcmean, data = lucid)
lucidneutralcontribution <- lm(contw ~ rgcmean, data = lucid)

m1giveblackout <- tidy(lucidblackcontribution, conf.int = TRUE, conf.level = .95)
m1giveblackout <- subset(m1giveblackout, term == "rgcmean")
m1giveblackout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m1supportblackout <- tidy(lucidblacksupport, conf.int = TRUE, conf.level = .95)
m1supportblackout <- subset(m1supportblackout, term == "rgcmean")
m1supportblackout$group <- "Predicted Value of RGC on 0-10 Support"

combined<-rbind(m1giveblackout, m1supportblackout)
combined$estimation <- combined$group
combined$groups[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combined$groups[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "

p <- ggplot(data = combined, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p<-p + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-6, 11)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Black Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p

m1giveneutralout <- tidy(lucidneutralcontribution, conf.int = TRUE, conf.level = .95)
m1giveneutralout <- subset(m1giveneutralout, term == "rgcmean")
m1giveneutralout$group <- "Predicted Value of RGC on $0-$10 Contributed"

m1supportneutralout <- tidy(lucidneutralsupport, conf.int = TRUE, conf.level = .95)
m1supportneutralout <- subset(m1supportneutralout, term == "rgcmean")
m1supportneutralout$group <- "Predicted Value of RGC on 0-10 Support"

combined2<-rbind(m1giveneutralout, m1supportneutralout)
combined2$estimation <- combined$group
combined2$groups[combined$group == "Predicted Value of RGC on 0-10 Support"] <- "   "
combined2$groups[combined$group == "Predicted Value of RGC on $0-$10 Contributed"] <- " "

p2 <- ggplot(data = combined2, aes(x = groups, y = estimate, ymin = conf.low, ymax = conf.high))
p2<-p2 + 
  geom_hline(yintercept = 0, color = "black", linetype='dashed') +
  geom_pointrange(aes(shape = estimation),position = position_dodge(width = 0.35), size=0.5)+
  coord_flip() + 
  scale_y_continuous(breaks=c(-6,-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), limits = c(-6, 11)) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank() 
  ) + 
  theme(legend.position='bottom', legend.text=element_text(size=12))+   
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black')) + 
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=12))+
  labs(title = "Race-Neutral Organization", 
       caption = "",  
       x = "", y = "") + 
  scale_color_manual(values=c("black", "gray")) +
  guides(color = guide_legend(nrow=2)) +
  guides(shape = guide_legend(nrow=2)) 

p2

pa<-cowplot::plot_grid(p, p2, align = "h", axis = "bt")
pa

pas<-cowplot::plot_grid(p + theme(legend.position="none"), 
                        p2+ theme(legend.position="none"), ncol = 2)
legend_b <- get_legend(p  +
                         theme(legend.position = "bottom"))

cowplot::plot_grid(pas, legend_b, ncol = 1, rel_heights = c(1, .15))
ggsave2("appendixa8.jpeg")

####Appendix figure A.14####
#lowess plot of support and contribution#
p_donate <- ggplot(df, aes(x = rgcmean)) +
  geom_jitter(aes(y = contb, colour = "contb")) + #spreads the points to reduce overplotting
  geom_smooth(aes(y = contb, colour = "contb"), 
              method = loess, se = TRUE, linetype = "solid") + #linear line
  geom_smooth(aes(y = contb, colour = "contb"), 
              method = lm, se = FALSE, linetype = "dashed") + #loess line
  geom_jitter(aes(y = contw, colour = "contw")) +
  geom_smooth(aes(y = contw, colour = "contw"), 
              method = loess, se = TRUE, linetype = "solid") + #linear line
  geom_smooth(aes(y = contw, colour = "contw"), 
              method = lm, se = FALSE, linetype = "dashed") + #loess line
  theme_classic() +
  labs(title = "Donation", 
       x = "RGC",
       y = "0-10 Dollars Donated") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(limits = c(0,1)) +
  scale_y_continuous(limits = c(0,10))+
  scale_colour_manual(values = c("red", "blue"),
                      labels=c('Black Organization', 'Race Neutral Organization'),
                      name = "")

p_support <- ggplot(df, aes(x = rgcmean)) +
  geom_jitter(aes(y = suppb, colour = "suppb")) + #spreads the points to reduce overplotting
  geom_smooth(aes(y = suppb, colour = "suppb"), 
              method = loess, se = TRUE, linetype = "solid") + #linear line
  geom_smooth(aes(y = suppb, colour = "suppb"), 
              method = lm, se = FALSE, linetype = "dashed") + #loess line
  geom_jitter(aes(y = suppw, colour = "suppw")) +
  geom_smooth(aes(y = suppw, colour = "suppw"), 
              method = loess, se = TRUE, linetype = "solid") + #linear line
  geom_smooth(aes(y = suppw, colour = "suppw"), 
              method = lm, se = FALSE, linetype = "dashed") + #loess line
  theme_classic() +
  labs(title = "Support", 
       x = "RGC",
       y = "0-10 Support") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(limits = c(0,1)) +
  scale_y_continuous(limits = c(0,10)) +
  scale_colour_manual(values = c("red", "blue"),
                      labels=c('Black Organization', 'Race Neutral Organization'),
                      name = "")
library(ggpubr)
figurea9 <- ggarrange(p_support, p_donate, ncol = 2, common.legend = TRUE, legend = "bottom")
figurea9
ggsave2("appendixa9.jpeg")



###descriptive statistics of experiment#
####Appendix table A.11####
#percent female#
mean(df$female)

#percent democrat#
df$democrat[df$pid == 3 & df$pidind == 2] <- 0
df$democrat[df$pid == 3 & df$pidind == 3] <- 0
df$democrat[df$pid == 2] <- 0
df$democrat[df$pid == 3 & df$pidind == 1] <- 1
df$democrat[df$pid == 1] <- 1
df$democrat[is.na(df$democrat)] <- 0
mean(df$democrat)

#median ideology#
median(df$ideo)

#median age#
median(df$y_born, na.rm = TRUE)

#median education#
median(df$edu, na.rm = TRUE)

#median income#
median(df$inc, na.rm = TRUE)


####balance check analysis###
####Appendix table A.12####
table(df$conditionvar)
library(doBy)
summaryBy(female ~ conditionvar, data = df, 
          FUN = list(mean, max, min, median, sd))
summaryBy(y_born ~ conditionvar, data = df, 
          na.rm = TRUE, FUN = list(mean, max, min, median, sd))
summaryBy(democrat ~ conditionvar, data = df, 
          na.rm = TRUE, FUN = list(mean, max, min, median, sd))
summaryBy(ideo ~ conditionvar, data = df, 
          na.rm = TRUE, FUN = list(mean, max, min, median, sd))
summaryBy(edu ~ conditionvar, data = df, 
          na.rm = TRUE, FUN = list(mean, max, min, median, sd))
summaryBy(inc ~ conditionvar, data = df, 
          na.rm = TRUE, FUN = list(mean, max, min, median, sd))

####Appendix figure A.16######
#Correlation Matrix Plot#
library(psych)
library(corrplot)
cor.table = cor(ndexp, use = "na.or.complete")
corrplot(cor.table, method = 'shade', order = 'AOE', diag = FALSE) 
setwd("/Users/arvindkrishnamurthy/Dropbox/Replication/contribution experiment/")
ggsave(filename="appendixa11.jpeg", device="jpeg", height=5, width=7, units="in", dpi=500)

#factor analysis#
principals<-principal(na.omit(ndexp))
principals$values
###should have either 3 or 4 factors###

#Appendix table A.22# 
fit <- principal(na.omit(ndexp), nfactors=3, rotate="varimax")
fit 

###factor 1: RIDIMP, Selfimage, Impself, ftblack, close
###factor 2: blackfate, keptback, linkfate, blackrights, rrdiscrim, autonomy, discrim (equal btwn 2/3)
###factor 3: blackorg, votediff, collective, power, workhard


#screeplot#
#we took out the scree plot from the appendix, but here it is
library(FactoMineR)
res.pca <- PCA(ndexp, graph = FALSE)
####
library(factoextra)
fviz_eig(res.pca, addlabels = TRUE, ylim = c(0, 50)) ####appendix figure A.13####
ggsave(filename="appendixa13.jpeg", device="jpeg", height=5, width=7, units="in", dpi=500)

####Appendix table A.24####
###factor regression###
df$affectidentity <- (rowMeans(df[c("ridimp", "selfimage", "impself", "ftblack", "close")], na.rm=TRUE))
df$commonfatediscrim <- (rowMeans(df[c("blackfate", "keptback", "linkfate", "blackrights", 
                                       "close", "autonomy", "rrdiscrim", "discrim")], na.rm=TRUE))
df$collectiveact <- (rowMeans(df[c("blackorg", "votediff", "collective", "power", 
                                   "workhardindv")], na.rm=TRUE))
df$blackorganization[df$conditionvar == 1] <- 1
df$blackorganization[df$conditionvar == 2] <- 0
df$contribution<-(rowMeans(df[c("contw", "contb")], na.rm = TRUE))
mfactor1<-lm(contribution ~ affectidentity*blackorganization + mturk + totnumday, data = df)
mfactor2<-lm(contribution ~ commonfatediscrim*blackorganization + mturk + totnumday, data = df)
mfactor3<-lm(contribution ~ collectiveact*blackorganization + mturk + totnumday, data = df)

summary(mfactor1)
summary(mfactor2)
summary(mfactor3)

library(stargazer)
stargazer(mfactor1, mfactor2, mfactor3, type = "html", out="tablea13.doc")


#interaction regression#
####Appendix table A.27####
df$income <- (df$inc - 1) / 15
df$contributions <- (rowMeans(df[c("contb", "contw")], na.rm=TRUE))
df$blackorg <- 0
df$blackorg[df$conditionexperiment == "Black Organization"] <- 1
mintnocontrols<-lm(contributions ~ rgcmean*blackorg, data = df)
summary(mintnocontrols)
###mturk controls regression
mint<-lm(contributions ~ rgcmean*blackorg + mturk + totnumday+income, data = df)
summary(mint)
stargazer(mint, mintnocontrols, type = "html", out="tablea21pt1.doc")

#gender control and mean re-centered regressions
####appendix table A.27 continued####
#mean centering interactions
#appendix#
df$rgcmeancenter <- df$rgcmean - 0.6820299
meancentered<- lm (contributions ~ rgcmeancenter*blackorg, data = df)
meancenteredgender <- lm (contributions ~ rgcmeancenter*blackorg + gender, data = df)
summary(meancentered)

meancenteredcontrols <- lm (contributions ~ rgcmeancenter*blackorg + mturk + totnumday + income + gender, data = df)
summary(meancenteredcontrols)
####gender, reweight ###
df$regressionweight[df$gender == 2] <- 1
df$regressionweight[df$gender == 1] <- 2.154696

meancenteredweight<- lm (contributions ~ rgcmeancenter*blackorg, data = df, weights = regressionweight)
summary(meancenteredweight)
meancenteredcontrols <- lm (contributions ~ rgcmeancenter*blackorg + mturk + totnumday + income + women, data = df)
summary(meancenteredcontrols)
#####table A.21pt2 output###
stargazer(meancenteredcontrols, meancenteredweight, type = "html", out="tablea21pt2.doc")

#interaction regression plot#
####figure A.11####
df$conditionexperiment[df$blackorg == 0] <- "White Organization"
df$conditionexperiment[df$blackorg == 1] <- "Black Organization"

mint<-lm(contributions ~ rgcmean*conditionexperiment + mturk + totnumday + income, data = df)
summary(mint)

mintnocontrols <- lm (contributions ~ rgcmean*conditionexperiment, data = df)
summary(mintnocontrols)

library(sjPlot)
p<-plot_model(mint, type = "pred", terms = c("rgcmean", "conditionexperiment"), colors = "bw", ci.style = c("bar"),
              axis.title = "Contribution ($0-$10)",
              title = "Contribution Study Interaction \n Model, No Controls"
)
p<-p+labs(x = "RGC")+scale_y_continuous(limits = c(0, 10))+ theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank(),
  panel.grid.major.x = element_blank(), 
  panel.grid.minor.x = element_blank() 
) + theme(legend.position='bottom')+ theme(plot.title = element_text(hjust = 0.5)) +theme(panel.background = element_rect(fill = 'white', color = 'black'))+
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=10))

p1<-plot_model(mintnocontrols, type = "pred", terms = c("rgcmean", "conditionexperiment"), colors = "bw", ci.style = c("bar"),
               axis.title = "Contribution ($0-$10)",
               title = "Contribution Study Interaction \n Model w/Controls")
p2<-p1+labs(x = "RGC")+scale_y_continuous(limits = c(0, 10))+theme(
  panel.grid.major.y = element_blank(),
  panel.grid.minor.y = element_blank(),
  panel.grid.major.x = element_blank(), 
  panel.grid.minor.x = element_blank() 
) + theme(legend.position='bottom')+   theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = 'white', color = 'black'))+
  theme(plot.title = element_text(size=12))+
  theme(legend.text = element_text(size=10))


pa<-cowplot::plot_grid(p, p2, align = "h", axis = "bt")
pa

pas<-cowplot::plot_grid(p + theme(legend.position="none"), 
                        p2+ theme(legend.position="none"), ncol = 2)
legend_b <- get_legend(p  +
                         theme(legend.position = "bottom"))

cowplot::plot_grid(pas, legend_b, ncol = 1, rel_heights = c(1, .15))
ggsave2("figurea.11.jpeg")
#test for multicolinearity after mean centering variable#
####Appendix Table A.20####
car::vif(meancenteredcontrols)




